<?php

namespace App\Http\Controllers\CmsApi;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;
use App\Lib\Response;
use App\Http\Controllers\BaseController;
use App\Http\Model\Hint;
use App\Http\Services\WishWheelServices;


class InnerWishWheelController extends BaseController
{
    protected $wishWheelService;

    public function __construct()
    {
        $this->wishWheelService = new WishWheelServices();

    }

    /**
     * 许愿转盘设置单次抽奖消耗数量
     */
    public function wishWheelSetNeedNum(Request $request)
    {
        $needNum = (int) $request->input('need_num', 0);
        if($needNum < 1) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        //锁
        $ifLock = redis_lock('WISHWHEEL:wishWheelSetNeedNum', 1);
        if (!$ifLock){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT);
        }

        $data = $this->wishWheelService->editWishWheelConfig(['need_num'=>$needNum]);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 许愿转盘设置档位抽取次数
     */
    public function wishWheelSetDrawNum(Request $request)
    {
        $draw_num_1 = (int) $request->input('draw_num_1', 0);
        $draw_num_2 = (int) $request->input('draw_num_2', 0);
        $draw_num_3 = (int) $request->input('draw_num_3', 0);
        $draw_img_1a = getUrlPath($request->input('draw_img_1a', ''));
        $draw_img_1b = getUrlPath($request->input('draw_img_1b', ''));
        $draw_img_2a = getUrlPath($request->input('draw_img_2a', ''));
        $draw_img_2b = getUrlPath($request->input('draw_img_2b', ''));
        $draw_img_3a = getUrlPath($request->input('draw_img_3a', ''));
        $draw_img_3b = getUrlPath($request->input('draw_img_3b', ''));
        if($draw_num_1<1 || $draw_num_2<1 || empty($draw_img_1a) || empty($draw_img_1b) || empty($draw_img_2a) || empty($draw_img_2b) || empty($draw_img_3a) || empty($draw_img_3b)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        //锁
        $ifLock = redis_lock('WISHWHEEL:wishWheelSetDrawNum', 1);
        if (!$ifLock){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT);
        }

        $data = $this->wishWheelService->editWishWheelConfig(['draw_num_1'=>$draw_num_1,'draw_num_2'=>$draw_num_2,'draw_num_3'=>$draw_num_3,'draw_img_1a'=>$draw_img_1a,'draw_img_1b'=>$draw_img_1b,'draw_img_2a'=>$draw_img_2a,'draw_img_2b'=>$draw_img_2b,'draw_img_3a'=>$draw_img_3a,'draw_img_3b'=>$draw_img_3b]);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 许愿转盘设置奖池开启/关闭
     */
    public function wishWheelSetPoolStatus(Request $request)
    {
        $poolId = (int) $request->input('pool_id', 0);
        $poolStatus = (int) $request->input('pool_status', 0);
        if(empty($poolId) || empty($poolStatus)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        //锁
        $key = 'WISHWHEEL:wishWheelPool';
        $ifLock = redis_lock($key, 60);
        if (!$ifLock){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT, '许愿转盘更新奖池中, 同一时间请勿重复请求');
        }

        $data = $this->wishWheelService->wishWheelSetPoolStatus($poolId, $poolStatus);
        redis_unlock($key);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 许愿转盘保存奖池配置
     */
    public function wishWheelSavePoolConfig(Request $request)
    {
        $poolId = (int) $request->input('pool_id', 0);
        $poolList = $request->input('pool_list');
        if(empty($poolId) || empty($poolList)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        $poolList = json_decode($poolList, true);
        if(empty($poolList)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //锁
        $ifLock = redis_lock('WISHWHEEL:wishWheelSavePoolConfig', 1);
        if (!$ifLock){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT);
        }

        $data = $this->wishWheelService->wishWheelSavePoolConfig($poolId, $poolList);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 许愿转盘新增奖池
     */
    public function wishWheelAddPool(Request $request)
    {
        $poolName = $request->input('pool_name', '');
        $minNum = (int) $request->input('min_num', 0);
        $maxNum = (int) $request->input('max_num', 0);
        $poolType = (int) $request->input('pool_type', 0);
        $poolType = empty($poolType) ? 1 : $poolType;
        $ifDefault = (int) $request->input('if_default', 2);
        $ifDefault = empty($ifDefault) ? 2 : $ifDefault;
        if(empty($poolName)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        if(in_array($poolType, [1]) && (empty($minNum) || empty($maxNum))) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //锁
        $ifLock = redis_lock('WISHWHEEL:wishWheelAddPool', 1);
        if (!$ifLock){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT);
        }

        $data = $this->wishWheelService->wishWheelAddPool($poolName, $minNum, $maxNum, $poolType, $ifDefault);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 许愿转盘编辑奖池
     */
    public function wishWheelEditPool(Request $request)
    {
        $poolId = $request->input('pool_id', 0);
        $poolName = $request->input('pool_name', '');
        $minNum = (int) $request->input('min_num', 0);
        $maxNum = (int) $request->input('max_num', 0);
        $ifDefault = (int) $request->input('if_default', 0);
        if(empty($poolId) || empty($poolName)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //锁
        $ifLock = redis_lock('WISHWHEEL:wishWheelEditPool', 1);
        if (!$ifLock){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT);
        }

        $data = $this->wishWheelService->wishWheelEditPool($poolId, $poolName, $minNum, $maxNum, $ifDefault);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 许愿转盘更新奖池缓存
     */
    public function wishWheelPoolReset(Request $request)
    {
        $poolId = (int) $request->input('pool_id', 0);
        if(empty($poolId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        //锁
        $key = 'WISHWHEEL:wishWheelPool';
        $ifLock = redis_lock($key, 3600);
        if (!$ifLock){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT, '许愿转盘更新奖池中, 同一时间请勿重复请求');
        }

        $data = $this->wishWheelService->wishWheelPoolReset($poolId);
        redis_unlock($key);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 许愿转盘奖池配置剩余数量
     */
    public function wishWheelPoolConfigNum(Request $request)
    {
        $poolId = (int) $request->input('pool_id', 0);
        if(empty($poolId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $data = $this->wishWheelService->wishWheelPoolConfigNum($poolId);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 许愿转盘新增白名单
     */
    public function wishWheelAddWhite(Request $request)
    {
        $userId = $request->input('user_id', 0);
        $giftId = $request->input('gift_id', 0);
        $adminId = $request->input('admin_id',0);
        if(empty($userId) || empty($giftId) || empty($adminId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //锁
        $ifLock = redis_lock('WISHWHEEL:wishWheelAddWhite', 1);
        if (!$ifLock){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT);
        }

        $data = $this->wishWheelService->wishWheelAddWhite($userId, $giftId, $adminId);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 许愿转盘删除白名单
     */
    public function wishWheelDelWhite(Request $request)
    {
        $whiteId = $request->input('white_id', 0);
        if(empty($whiteId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //锁
        $ifLock = redis_lock('WISHWHEEL:wishWheelDelWhite', 1);
        if (!$ifLock){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT);
        }

        $data = $this->wishWheelService->wishWheelDelWhite($whiteId);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 新增许愿转盘展示礼物
     */
    public function wishWheelAddGift(Request $request)
    {
        $giftId = (int) $request->input('gift_id', 0);
        $sort = (int) $request->input('sort', 0);
        if(empty($giftId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $data = $this->wishWheelService->wishWheelAddGift($giftId, $sort);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 许愿转盘删除展示礼物
     */
    public function wishWheelDelGift(Request $request)
    {
        $id = (int) $request->input('id', 0);
        if(empty($id)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $data = $this->wishWheelService->wishWheelDelGift($id);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 许愿转盘新增个人池用户
     */
    public function wishWheelAddPoolUser(Request $request)
    {
        $userId = $request->input('user_id', 0);
        $adminId = $request->input('admin_id',0);
        $poolId = (int) $request->input('pool_id', 0);
        $outDiamondNum = (int) $request->input('out_diamond_num', 0);
        if(empty($userId) || empty($poolId) || empty($adminId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //锁
        $lockKey = 'WISHWHEEL:poolUserMake';
        if (!redis_lock($lockKey, 300)){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT, '个人池用户操作处理中, 同一时间请勿重复请求');
        }

        $data = $this->wishWheelService->wishWheelAddPoolUser($userId, $poolId, $outDiamondNum, $adminId);
        redis_unlock($lockKey);

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * 许愿转盘删除个人池用户
     */
    public function wishWheelDelPoolUser(Request $request)
    {
        $id = (int) $request->input('id', 0);
        $adminId = $request->input('admin_id',0);
        if(empty($id) || empty($adminId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //锁
        $lockKey = 'WISHWHEEL:poolUserMake';
        if (!redis_lock($lockKey, 100)){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT, '个人池用户操作处理中, 同一时间请勿重复请求');
        }

        $data = $this->wishWheelService->wishWheelDelPoolUser($id, $adminId);
        redis_unlock($lockKey);

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    //查看个人奖池剩余数
    public function wishWheelPoolSelfConfigNum(Request $request){
        $poolId = (int) $request->input('pool_id', 0);
        $userId = (int) $request->input('user_id', 0);
        if(empty($poolId) || empty($userId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $data = $this->wishWheelService->wishWheelPoolSelfConfigNum($poolId, $userId);
        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * 许愿转盘新增奖池白名单
     */
    public function wishWheelAddPoolWhite(Request $request)
    {
        $userId = $request->input('user_id', 0);
        $giftId = $request->input('gift_id', 0);
        $adminId = $request->input('admin_id',0);
        if(empty($userId) || empty($giftId) || empty($adminId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //锁
        $ifLock = redis_lock('WISHWHEEL:wishWheelAddPoolWhite', 1);
        if (!$ifLock){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT);
        }

        $data = $this->wishWheelService->wishWheelAddPoolWhite($userId, $giftId, $adminId);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 许愿转盘删除奖池白名单
     */
    public function wishWheelDelPoolWhite(Request $request)
    {
        $whiteId = $request->input('white_id', 0);
        if(empty($whiteId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //锁
        $ifLock = redis_lock('WISHWHEEL:wishWheelDelPoolWhite', 1);
        if (!$ifLock){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT);
        }

        $data = $this->wishWheelService->wishWheelDelWhite($whiteId);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 许愿转盘删除奖池白名单
     */
    public function wishWheelCancelPoolWhite(Request $request)
    {
        $whiteId = $request->input('white_id', 0);
        if(empty($whiteId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //锁
        $ifLock = redis_lock('WISHWHEEL:wishWheelCancelPoolWhite', 1);
        if (!$ifLock){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT);
        }

        $data = $this->wishWheelService->wishWheelCancelWhite($whiteId);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 许愿转盘奖池更新阀值
     */
    public function wishWheelSetUserPoolConfig(Request $request)
    {
        $poolId = (int) $request->input('pool_id', 0);
        $inProfitLoss = (int) $request->input('in_profit_loss', 0);
        $inProfitRate = (int) $request->input('in_profit_rate', 0);
        $outProfitRate = (int) $request->input('out_profit_rate', 0);
        if(empty($poolId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //锁
        $ifLock = redis_lock('WISHWHEEL:wishWheelSetUserPoolConfig', 1);
        if (!$ifLock){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT);
        }

        $data = $this->wishWheelService->wishWheelSetUserPoolConfig($poolId, ['in_profit_loss'=>$inProfitLoss, 'in_profit_rate'=>$inProfitRate, 'out_profit_rate'=>$outProfitRate]);

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * 许愿转盘新增抽取奖池
     */
    public function wishWheelAddExtractPool(Request $request)
    {
        $poolName = $request->input('pool_name', '');
        $poolId = (int) $request->input('pool_id', 0);
        if(empty($poolName) || empty($poolId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //锁
        $ifLock = redis_lock('WISHWHEEL:wishWheelAddExtractPool', 1);
        if (!$ifLock){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT);
        }

        $data = $this->wishWheelService->wishWheelAddExtractPool($poolName, $poolId);

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * 许愿转盘编辑抽取奖池
     */
    public function wishWheelEditExtractPool(Request $request)
    {
        $extractPoolId = $request->input('extract_pool_id', 0);
        $poolName = $request->input('pool_name', '');
        if(empty($extractPoolId) || empty($poolName)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //锁
        $ifLock = redis_lock('WISHWHEEL:wishWheelEditExtractPool', 1);
        if (!$ifLock){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT);
        }

        $data = $this->wishWheelService->wishWheelEditExtractPool($extractPoolId, $poolName);

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * 许愿转盘设置抽取奖池开启/关闭
     */
    public function wishWheelSetExtractPoolStatus(Request $request)
    {
        $extractPoolId = $request->input('extract_pool_id', 0);
        $poolStatus = (int) $request->input('pool_status', 0);
        if(empty($extractPoolId) || empty($poolStatus)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //锁
        $ifLock = redis_lock('WISHWHEEL:wishWheelSetExtractPoolStatus', 1);
        if (!$ifLock){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT);
        }

        $data = $this->wishWheelService->wishWheelSetExtractPoolStatus($extractPoolId, $poolStatus);

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * 许愿转盘新增抽取奖池用户
     */
    public function wishWheelAddExtractPoolUser(Request $request)
    {
        $extractPoolId = $request->input('extract_pool_id', 0);
        $userId = $request->input('user_id', 0);
        $ifConfirm = $request->input('if_confirm', 0);
        $adminId = $request->input('admin_id',0);
        if(empty($extractPoolId) || empty($userId) || empty($adminId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //锁
        $ifLock = redis_lock('WISHWHEEL:wishWheelAddExtractPoolUser', 1);
        if (!$ifLock){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT);
        }

        $data = $this->wishWheelService->wishWheelAddExtractPoolUser($userId, $extractPoolId, $adminId, $ifConfirm);

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * 许愿转盘删除抽取奖池用户
     */
    public function wishWheelDelExtractPoolUser(Request $request)
    {
        $id = (int) $request->input('id', 0);
        $adminId = $request->input('admin_id',0);
        if(empty($id) || empty($adminId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //锁
        $ifLock = redis_lock('WISHWHEEL:wishWheelDelExtractPoolUser', 1);
        if (!$ifLock){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT);
        }

        $data = $this->wishWheelService->wishWheelDelExtractPoolUser($id, $adminId);

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * 许愿转盘新增抽取奖池礼物
     */
    public function wishWheelAddExtractPoolGift(Request $request)
    {
        $extractPoolId = $request->input('extract_pool_id', 0);
        $extractInfo = $request->input('extract_info', 0);
        $extractInfo = json_decode($extractInfo, true);
        $adminId = $request->input('admin_id',0);
        if(empty($extractPoolId) || empty($extractInfo) || empty($adminId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        //锁
        $lockKey = 'WISHWHEEL:wishWheelAddExtractPoolGift';
        if (!redis_lock($lockKey, 600)){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT, '许愿转盘新增抽取奖池礼物操作处理中, 同一时间请勿重复请求');
        }

        $data = $this->wishWheelService->wishWheelAddExtractPoolGift($extractPoolId, $extractInfo, $adminId);
        redis_unlock($lockKey);

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * 许愿转盘抽取奖池剩余数量
     */
    public function wishWheelPoolExtractLessNum(Request $request)
    {
        $extractPoolId = $request->input('extract_pool_id', 0);
        if(empty($extractPoolId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $data = $this->wishWheelService->wishWheelPoolExtractLessNum($extractPoolId);

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

}